package xyz.heyaoshare.core.controller.system.dict;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import xyz.heyaoshare.annotation.OperationLog;
import xyz.heyaoshare.common.entity.bo.Result;
import xyz.heyaoshare.common.entity.bo.ResultPage;
import xyz.heyaoshare.core.controller.system.dict.vo.req.*;
import xyz.heyaoshare.core.controller.system.dict.vo.resp.*;
import xyz.heyaoshare.core.service.system.DictDataService;
import xyz.heyaoshare.core.service.system.DictTypeService;

import java.util.List;

import static xyz.heyaoshare.common.constants.VersionManager.V001;

/**
 * 字典相关
 * @author YueHe
 * @version 0.0.1
 * @since 2025/5/9 下午4:52
 */
@RestController
@RequestMapping(V001 + "/system/dict")
@Tag(name = "字典相关", description = "包含字典类型以及字典数据值")
public class DictController {

    @Resource
    private DictTypeService dictTypeService;
    @Resource
    private DictDataService dictDataService;

    @OperationLog
    @GetMapping("type/page")
    @Operation(summary = "字典类型分页")
    public Result<ResultPage<DictTypePageRespVO>> selectPage(@ParameterObject @Valid DictTypePageReqVO reqVO) {
        ResultPage<DictTypePageRespVO> items = dictTypeService.selectPage(reqVO);
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping("type/list")
    @Operation(summary = "字典类型列表")
    public Result<List<DictTypeListRespVO>> selectList(@ParameterObject @Valid DictTypeListReqVO reqVO) {
        List<DictTypeListRespVO> items = dictTypeService.selectList(reqVO);
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping("type")
    @Operation(summary = "字典类型详情")
    public Result<DictTypeDetailRespVO> selectDetail(@ParameterObject @Valid DictTypeDetailReqVO reqVO) {
        DictTypeDetailRespVO item = dictTypeService.selectDetail(reqVO);
        return Result.ok(item);
    }

    @OperationLog
    @PostMapping("type/insert")
    @Operation(summary = "新增字典类型")
    public Result<Boolean> insert(@RequestBody @Valid DictTypeInsertReqVO reqVO) {
        Boolean status = dictTypeService.insert(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @DeleteMapping("type/delete")
    @Operation(summary = "删除字典类型")
    public Result<Boolean> delete(@RequestBody @Valid DictTypeDeleteReqVO reqVO) {
        Boolean status = dictTypeService.delete(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @PutMapping("type/update")
    @Operation(summary = "更新字典类型")
    public Result<Boolean> updateData(@RequestBody @Valid DictTypeUpdateReqVO reqVO) {
        Boolean status = dictTypeService.updateData(reqVO);
        return Result.ok(status);
    }


    // ==================================== 字典数据相关接口 =====================================
    @OperationLog
    @GetMapping("data/page")
    @Operation(summary = "字典数据分页")
    public Result<ResultPage<DictDataPageRespVO>> selectPage(@ParameterObject @Valid DictDataPageReqVO reqVO) {
        ResultPage<DictDataPageRespVO> items = dictDataService.selectPage(reqVO);
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping("data/list")
    @Operation(summary = "字典数据列表", description = "所有正常的字典数据，前端可请求该接口后缓存到前端，定期调用 或 在字典相关页面发生变更时调用即可")
    public Result<List<DictDataListRespVO>> selectList(@ParameterObject @Valid DictDataListReqVO reqVO) {
        List<DictDataListRespVO> items = dictDataService.selectList(reqVO);
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping("data/simpleList")
    @Operation(summary = "字典统一数据列表", description = "所有正常的字典数据，前端可请求该接口后缓存到前端，定期调用 或 在字典相关页面发生变更时调用即可")
    public Result<List<DictSimpleListRespVO>> simpleList() {
        List<DictSimpleListRespVO> items = dictDataService.simpleList();
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping("data")
    @Operation(summary = "字典数据详情")
    public Result<DictDataDetailRespVO> selectDetail(@ParameterObject @Valid DictDataDetailReqVO reqVO) {
        DictDataDetailRespVO item = dictDataService.selectDetail(reqVO);
        return Result.ok(item);
    }

    @OperationLog
    @PostMapping("data/insert")
    @Operation(summary = "新增字典数据")
    public Result<Boolean> insert(@RequestBody @Valid DictDataInsertReqVO reqVO) {
        Boolean status = dictDataService.insert(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @DeleteMapping("data/delete")
    @Operation(summary = "删除字典数据")
    public Result<Boolean> delete(@RequestBody @Valid DictDataDeleteReqVO reqVO) {
        Boolean status = dictDataService.delete(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @PutMapping("data/update")
    @Operation(summary = "更新字典数据")
    public Result<Boolean> updateData(@RequestBody @Valid DictDataUpdateReqVO reqVO) {
        Boolean status = dictDataService.updateData(reqVO);
        return Result.ok(status);
    }

    @OperationLog
    @GetMapping("data/list/by-type")
    @Operation(summary = "统一获取字典数据", description = "前端调用")
    public Result<List<DictDataByTypeRespVO>> getByType(@ParameterObject @Valid DictDataByTypeReqVO reqVO) {
        List<DictDataByTypeRespVO> items = dictDataService.getByType(reqVO);
        return Result.ok(items);
    }

    @OperationLog
    @GetMapping("data/list/by-type-name")
    @Operation(summary = "统一获取字典数据-通过类型名称", description = "前端调用")
    public Result<List<DictDataByTypeRespVO>> getByTypeName(@ParameterObject @Valid DictDataByTypeNameReqVO reqVO) {
        List<DictDataByTypeRespVO> items = dictDataService.getByTypeName(reqVO);
        return Result.ok(items);
    }

}